package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.OrderReturnBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.OrderReturnMapper;
import com.molichuxing.services.infrastructure.dto.request.create.OrderReturnCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OrderRecycleCarsModifyDto;
import com.molichuxing.services.infrastructure.dto.response.OrderReturnDto;
import com.molichuxing.services.infrastructure.service.OrderReturnService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单还车表 服务实现类
 * </p>
 *
 * @author renzhengyu
 * @since 2019-08-12
 */
@Service("orderReturnService")
public class OrderReturnServiceImpl implements OrderReturnService {
    private static final Logger logger = LoggerFactory.getLogger(OrderReturnServiceImpl.class);

    @Resource
    private OrderReturnMapper orderReturnMapper;

    /**
     * 根据订单号查询还车信息（不包含收车失败信息）
     *
     * @param orderCodes 订单号-list
     * @return
     */
    @Override
    public Map<Long, OrderReturnDto> getMapByOrderCodes(List<Long> orderCodes) {
        Map<Long, OrderReturnDto> result = null;
        if (orderCodes == null || orderCodes.size() <= 0) {
            return result;
        }
        List<OrderReturnBean> beanList = orderReturnMapper.getListByOrderCodesAlready(orderCodes);
        if (beanList == null || beanList.size() <= 0) {
            return result;
        }
        result = new HashMap<>();
        for (OrderReturnBean orderReturnBean : beanList) {
            result.put(orderReturnBean.getOrderCode(), Convert.toOrderReturnDto(orderReturnBean));
        }

        return result;
    }

    /**
     * 查询订单还车信息（包含收车失败信息）
     *
     * @param orderCode 订单号
     * @return
     */
    @Override
    public OrderReturnDto getOrderReturnAll(Long orderCode) {
        return Convert.toOrderReturnDto(orderReturnMapper.getByOrderCode(orderCode));
    }

    /**
     * 查询订单还车信息（不包含收车失败信息）
     *
     * @param orderCode 订单号
     * @return
     */
    @Override
    public OrderReturnDto getOrderReturn(Long orderCode) {
        return Convert.toOrderReturnDto(orderReturnMapper.getByOrderCodeAlready(orderCode));
    }

    /**
     * 订单还车新增
     *
     * @param createDto
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Boolean create(OrderReturnCreateDto createDto) {
        return orderReturnMapper.create(Convert.toOrderReturnBean(createDto)) > 0;
    }

    /**
     * 查询30天内还车的车辆id，去重（不包含收车失败信息）
     *
     * @return
     */
    @Override
    public List<OrderReturnDto> getOrderReturnDtoListByNow() {
        return Convert.toOrderReturnDto(orderReturnMapper.getOrderReturnDtoListByNow());
    }

    /**
     * 收车修改
     *
     * @param modifyDto
     * @return
     */
    @Override
    public boolean modifyRecycle(OrderRecycleCarsModifyDto modifyDto) {
        return orderReturnMapper.modifyRecycle(Convert.toOrderReturnBean(modifyDto)) > 0;
    }

}
